#! /usr/bin/env python
#
# Name:         FTPy.py
# Date:         1/23/05
# Author:       Jordan Haven
# Website:      http://www.jhaven.fwsh.com
# Description:  FTPy is an FTP client. For
#               more information, read the
#               README included with this
#               distribution.
#
# TODO:
#       >>> Add support for MGET
#       >>> Add support for MPUT
#       >>> Add support for ASCII / Binary connections
#       >>> Add more intelligent error handling

# Necessary imports
from ftplib import FTP
import os
import sys
import getpass
from commands import main
import parser

startupmsg = """
Welcome to EasyCMX!
"""

# Get root
root = os.getcwd()
platform = sys.platform
if (platform == "win32"):
    root = root + "\\"
else:
    root = root + "/"

# Go to home environment, and set the default download directory
home = os.getenv('HOME') or ''
#os.chdir(home)
try:
    os.mkdir('downloads')
    os.chdir('downloads')
except:
    pass
downloads = os.getcwd()

# Log the user in
def login(host, port, user, passwd):
    fail = 0
    # Check for valid host, user and password and supply appropriate errors
    try:
        ftp = FTP(host)
        ftp.connect(host)
        try:
            ftp.login(user, passwd)
        except:
            print "ERROR >> Wrong username / password.\n"
            fail = fail + 1
    except:
        print "ERROR >> " + host + " on port " + str(port) + " is not valid.\n"
        fail = fail + 1
        
    # If everything is OK, continue, else startover
    if (fail != 1):
        print ftp.getwelcome()
        main(ftp, host, user)
    else:
        start()

# Get pass of not passed through command line
def getPass(host, port, user):
    passwd = raw_input("Pass: ")
    passwd = passwd.strip()
    login(host, port, user, passwd)

# Get username if not passed through command line
def getUser(host, port):
    user = raw_input("User: ")
    user = user.strip()
    getPass(host, port, user)
    
# Get host info if not passed through command line
def getHost():
    host = raw_input("Host: ")
#    try:
#        port = input("Port: ")
#    except:
#        port = 21
#        pass
    port = 21
    host = host.strip()
    getUser(host, port)

# Check for arguments
def argCheck():
    args = len(sys.argv)
    if args < 8:
        getHost()
    else:
        host = sys.argv[1]
        if args > 2:
            port = sys.argv[2]
            if args > 3:
                user = sys.argv[3]
                if args >= 4:
                    passwd = sys.argv[4]
                    login(host, port, user, passwd)
                else:
                    getPass(host, port, user)
            else:
                getUser(host, port)                    
        else:
            port = 21
        getUser(host, port)

# The starting point for FTPy
def start():
    print startupmsg
    argCheck()



# Start FTPy on launch
start()
